package com.xiaoxu.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaoxu.spzx.manager.service.CategoryBrandService;
import com.xiaoxu.spzx.model.dto.product.CategoryBrandDto;
import com.xiaoxu.spzx.model.entity.product.Brand;
import com.xiaoxu.spzx.model.entity.product.CategoryBrand;
import com.xiaoxu.spzx.model.vo.common.Result;
import com.xiaoxu.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/categoryBrand")
public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;


    //根据分类id查询 该分类下的品牌信息
    @GetMapping("/findBrandByCategoryId/{id}")
    public Result findBrandCategoryId(@PathVariable("id") Long id){
        List<Brand> list = categoryBrandService.findBrandCategoryId(id);
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }
    //再次声明 Get请求 不适用RequsetBody
    @GetMapping("/{page}/{limit}")
    public Result findCategoryBrandByPage(@PathVariable Integer page,
                                          @PathVariable Integer limit,
                                           CategoryBrandDto categoryBrandDto){

      PageInfo<CategoryBrand> pageInfo =  categoryBrandService.findByPage(page,
              limit,
              categoryBrandDto);


      return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
    // TODO添加
    @PostMapping("/save")
    public Result save(@RequestBody CategoryBrand categoryBrand) {
        categoryBrandService.save(categoryBrand);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    // TODO修改

    @PutMapping("/updateById")
    public Result updateById(@RequestBody CategoryBrand categoryBrand){

        categoryBrandService.update(categoryBrand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }




    //删除

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id){

        categoryBrandService.delete(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
